package info.liuxing.bean;

import org.springframework.beans.factory.annotation.Value;

import java.util.Date;
import java.util.Objects;

public class Person {

    /**
     * 使用@Value赋值；
     * 1、基本数值
     * 2、可以写SpELl: #{}
     * 3、可以写${}: 取出配置文件中的值(在运行环境变量里面的值)
     *
     */
    @Value("张三")
    private String name;
    @Value("#{20-2}")
    private Integer age;

    private Date createdAt;

    @Value("${person.sex}")
    private String sex;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
        this.createdAt = new Date();
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("{");
        sb.append("\"name\":\"")
                .append(name).append('\"');
        sb.append(",\"age\":")
                .append(age);
        sb.append(",\"sex\":")
                .append(sex);
        if (Objects.nonNull(createdAt)) {
            sb.append(",\"createdAt\":\"")
                    .append(createdAt.getTime()).append('\"');
        } else {
            sb.append(",\"createdAt\":\"")
                    .append(createdAt).append('\"');
        }
        sb.append('}');
        return sb.toString();
    }
}
